package com.situ.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.springboot.mapper.BlogMapper;
import com.situ.springboot.pojo.Blog;
import com.situ.springboot.pojo.query.BlogQuery;
import com.situ.springboot.pojo.vo.BlogVO;
import com.situ.springboot.service.IBlogService;
import com.situ.springboot.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

// Alt+Enter
@Service
public class BlogServiceImpl implements IBlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public List<Blog> list1() {
        return blogMapper.list1();
    }

    @Override
    public PageResult list(BlogQuery blogQuery) {
        //开启分页
        PageHelper.startPage(blogQuery.getPage(), blogQuery.getLimit());
        //1、查找当前页的数据
        List<BlogVO> list = blogMapper.list(blogQuery);
        //2、查询总的数量
        PageInfo pageInfo = new PageInfo(list);
        int totalCount = (int) pageInfo.getTotal();

        return new PageResult(0, "", totalCount, list);
    }

    @Override
    public void deleteById(Integer id) {
        blogMapper.deleteById(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        // delete from student where id in(1,2,34)
        for (Integer id : ids) {
            blogMapper.deleteById(id);
        }
    }

    @Override
    public void add(Blog blog) {
        blogMapper.add(blog);
    }

    @Override
    public Blog selectById(Integer id) {
        return blogMapper.selectById(id);
    }

    @Override
    public void update(Blog blog) {
        blogMapper.update(blog);
    }
}
